﻿using Digitalmes.Common.Utils;

namespace Digitalmes.Common.Extensions;

/// <summary>
/// <see cref="string"/> 扩展方法
/// </summary>
public static class StringExtension
{
    const string DefaultEncryptKey2 = "123@qwe!";

    /// <summary>
    /// 加密密钥，默认取“masuit”的MD5值
    /// </summary>
    private static readonly string DefaultEncryptKey = CryptoUtils.MDString2("masuit");

    /// <summary>
    /// 对称加密算法AES(块式加密算法)
    /// </summary>
    /// <param name="str">待加密字符串</param>
    /// <returns></returns>
    public static string AESEncrypt(this string str)
    {
        return CryptoUtils.AESEncrypt(str, DefaultEncryptKey);
    }

    public static string AESEncrypt(this string str, string key, CipherMode mode)
    {
        return CryptoUtils.AESEncrypt(str, key, mode);
    }

    public static string AESDecrypt(this string str)
    {
        return CryptoUtils.AESDecrypt(str, DefaultEncryptKey);
    }

    public static string AESDecrypt(this string str, string key, CipherMode mode)
    {
        return CryptoUtils.AESDecrypt(str, key, mode);
    }

    /// <summary>
    /// 对称加密算法DES加密，加密密钥必须为8位
    /// </summary>
    /// <param name="str">待加密字符串</param>
    /// <returns></returns>
    public static string DESEncrypt(this string str)
    {
        return CryptoUtils.DESEncrypt(str, DefaultEncryptKey);
    }

    /// <summary>
    ///  DES解密算法，密钥为8位
    /// </summary>
    /// <param name="str">需要解密的字符串</param>
    /// <returns></returns>
    public static string DESDecrypt(this string str)
    {
        return CryptoUtils.DESDecrypt(str, DefaultEncryptKey);
    }

    /// <summary>
    /// 对字符串进行MD5摘要
    /// </summary>
    /// <param name="str">需要摘要的字符串</param>
    /// <returns></returns>
    public static string MDString(this string str)
    {
        return CryptoUtils.MDString(str);
    }
}
